package offer;

/**
 * @Author Elephas
 * @Date 2022/1/14
 **/
public class ReplaceSpace {
    public String replaceSpace(String s){
        String ans = "";
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ' '){
                ans += "%20";
            }else{
                ans += s.charAt(i);
            }
        }
        return ans;
    }

    /*
        解法二：字符数组
     */
    public String replaceSpace2(String s){
        int len = 0;
        char[] array = new char[s.length()*3];
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ' '){
                array[len] = '%'; len++;
                array[len] = '2'; len++;
                array[len] = '0'; len++;
            }else{
                array[len] = s.charAt(i); len++;
            }
        }
        return String.copyValueOf(array,0,len);

    }

}
